package com.heima.wemedia.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 自媒体素材表(WmMaterial)表控制层
 *
 * @author makejava
 * @since 2022-04-10 10:54:32
 */
@RestController
@RequestMapping("/api/v1/material")
@Api(value = "自媒体素材管理", tags = "自媒体", description = "自媒体素材管理API")
public class WmMaterialController {
    /**
     * 服务对象
     */
    @Resource
    private WmMaterialService wmMaterialService;


    /**
     * 自媒体素材上传
     *
     * @param multipartFile
     * @return
     */
    @PostMapping(path = "/upload_picture")
    @ApiOperation("自媒体素材上传")
    public ResponseResult upload(MultipartFile multipartFile) {
        return wmMaterialService.upload(multipartFile);
    }

    /**
     * 素材列表查询
     *
     * @param dto
     * @return
     */
    @PostMapping(path = "/list")
    @ApiOperation("自媒体素材列表分页查询")
    public ResponseResult list(@RequestBody WmMaterialDto dto) {
        return wmMaterialService.queryByPage(dto);
    }


    @GetMapping("/del_picture/{id}")
    @ApiOperation("自媒体素材删除")
    public ResponseResult deleteById(@PathVariable Long id) {

        return wmMaterialService.deleteById(id);

    }

    @GetMapping("/collect/{id}")
    @ApiOperation("收藏图片")

    public ResponseResult collectById(@PathVariable Long id) {

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        wmMaterial.setIsCollection(1);
        return wmMaterialService.update(wmMaterial);
    }

    @GetMapping("/cancel_collect/{id}")
    @ApiOperation("取消收藏图片")

    public ResponseResult cancelCollectById(@PathVariable Long id) {

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        wmMaterial.setIsCollection(0);
        return wmMaterialService.update(wmMaterial);


    }
}
